import { Component, Label, Sprite, SpriteFrame, _decorator, assetManager, error, find, resources, Node, Vec2, Vec3, Texture2D, EventTouch } from "cc";

const { ccclass, property } = _decorator;

//https://blog.csdn.net/weixin_41997753/article/details/122661210
@ccclass
export default class Helloworld extends Component {

    @property(Label)
    label: Label = null;

    @property
    text: string = 'hello';
    @property(Sprite)
    spriteFrame: SpriteFrame = null;



    start() {
        // init logic
        this.label.string = this.text;
        this.openCountDownTime();

        // //Bundle 分包
        // let path = "http://192.168.122.167:5001/remote/BundleFile";
        // assetManager.loadBundle(path, { version: "7613a" }, (err, bundle) => {
        //     console.log(`---start-loadBundle-err:${err}, bundle:${bundle}`)
        //     if(!err && bundle){
        //         bundle.load('sprite2',Texture2D,(err,spriteFrame:Texture2D)=>{
        //             console.log(`---start-load-err:${err}, spriteFrame:${spriteFrame}`)
        //             // let tempNode: Node = new Node();
        //             // tempNode.parent = this.node;
        //             // tempNode.position = Vec3.ZERO;
        //             // tempNode.addComponent(Sprite).spriteFrame = new SpriteFrame(spriteFrame);
        //         });
        //     }
        // });
    }

    //倒计时模块
    private openCountDownTime() {
        let time: number = 6;
        this.label.string = time?.toString();
        let callback: Function = () => {
            if (time <= 0) {
                time = 0;
                this.loadSpriteAsset();
                return this.unschedule(callback);//倒计时完成
            }
            time--;
            error(time);
            this.label.string = Math.max(time, 0)?.toString();
        }
        this.schedule(callback, 1);
    }

    private loadSpriteAsset(): void {
        let path: string = 'sprite1';
        let sprite: Sprite = find('cocos', this.node)?.getComponent(Sprite);
        resources.load(path, SpriteFrame, (err: Error, spriteFrame: SpriteFrame) => {
            if (sprite) {
                sprite.spriteFrame = spriteFrame;
            }
        })
    }
}
